
//自己实现单链表
public class LinkedListMy implements ILinkedList{
    //获取链表头节点
    public ListNode head;

    //获取链表长度
    @Override
    public int GetSize() {
        ListNode cur = head;
        int len = 1;
        while(cur.next != null){
            len ++;
            cur = cur.next;
        }
        return len;
    }

    //遍历链表并打印
    @Override
    public void DisPlay() {
        ListNode cur = head;
        while(cur != null){
            System.out.println(cur.val + " ");
            cur = cur.next;
        }
    }

    //头插
    @Override
    public ListNode AddFront(int val) {
        //先出创建节点
        ListNode node = new ListNode(val);
        //无论头节点是否为空
        node.next = head;
        return head;
    }

    //尾插
    @Override
    public ListNode AddBack(int val) {
        //创建新节点
        ListNode node = new ListNode(val);
        //找到最后一个节点
        ListNode cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        //此时cur为尾节点
        cur.next = node;

        return head;
    }

    @Override
    public ListNode AddIndex(int pos,int val) {
        ListNode node = new ListNode(val);
        ListNode cur = head;
        while(cur.next != null && pos != 0){
            cur = cur.next;
            pos --;
        }

        return null;
    }

    @Override
    public ListNode findIndexSubOne() {
        return null;
    }

    @Override
    public boolean contain() {
        return false;
    }

    @Override
    public ListNode DeleteVal() {
        return null;
    }

    @Override
    public boolean IsEmpty() {
        return false;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null){
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }
}
